package ru.spb.osll.myfit.client.widget;

import ru.spb.osll.myfit.client.PageMode;
import ru.spb.osll.myfit.common.Define;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class DefineWidget extends PageElementWidget {
  private TextBox m_varNameTextBox;
  private TextBox m_varValueTextBox;
  private String m_varName;
  private String m_varValue; 
  private Label m_valueLable;
  private Label m_variableLabel;
  private FlexTable m_mainPanel;
  
  public DefineWidget(GWTWidgetFactory gwtWidgetFactory, Define define) {
    super(gwtWidgetFactory);
    sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    createPopupMenu();

    m_varName = define.getNameVariable();
    if (m_varName.trim().isEmpty()) m_varName = " ";

    m_varValue = define.getValueVariable();
    if (m_varValue.trim().isEmpty()) m_varValue = " ";

    initWidget(createWidget());
    m_mode = PageMode.EDIT;
  }

  private Widget createWidget() {
    m_mainPanel = m_gwtWidgetFactory.createFlexTable();
    
    Label header = new Label("variable defined: ");
    header.setStyleName("DefineHeaderStyle");
    
    Label equalLabal = new Label("=");
    equalLabal.setStyleName("DefineHeaderStyle");
    
    m_varNameTextBox = new TextBox();
    m_varNameTextBox.setStyleName("DefineVariableNameStyle");
    m_varNameTextBox.setText(m_varName);

    m_varValueTextBox = new TextBox();
    m_varValueTextBox.setStyleName("DefineVaribleValueStyle");
    m_varValueTextBox.setText(m_varValue);

    m_valueLable = new Label("");
    m_valueLable.setStyleName("DefineVariableLabelStyle");
    
    m_variableLabel = new Label("");
    m_variableLabel.setStyleName("DefineVariableLabelStyle");
    
    
    m_varValueTextBox.addChangeListener(new TextBoxOnChangePrivateListener());
    m_varNameTextBox.addChangeListener(new TextBoxOnChangePrivateListener());

    m_mainPanel.setWidget(0, 0, header);
    m_mainPanel.setWidget(0, 1, m_varNameTextBox);
    m_mainPanel.setWidget(0, 2, equalLabal);
    m_mainPanel.setWidget(0, 3, m_varValueTextBox);

    return m_mainPanel;
  }

  public PageElement getPageElement() {
    return new Define(m_varName, m_varValue);
  }

  public PageElementType getType() {
    return PageElementType.DEFINE;
  }

  public void setOpen(boolean isOpen) {
    // no action needed.
  }

  public void setMode(PageMode mode) {
    super.setMode(mode);
    if (mode == PageMode.EDIT) {
      m_mainPanel.setWidget(0, 1, m_varNameTextBox);
      m_mainPanel.setWidget(0, 3, m_varValueTextBox);
    } else {
      m_valueLable.setText(m_varValueTextBox.getText());
      m_variableLabel.setText(m_varNameTextBox.getText());
      m_mainPanel.setWidget(0, 1, m_variableLabel);
      m_mainPanel.setWidget(0, 3, m_valueLable);
    }
  }

  public String getName() {
    return m_varName;
  }

  public String getValue() {
    return m_varValue;
  }

  private class TextBoxOnChangePrivateListener implements ChangeListener {
    public void onChange(Widget sender) {
      TextBox tb = (TextBox) sender;
      String str = tb.getText().trim();
      if (str.isEmpty()) str = " ";
      tb.setText(str);

      String newVariableName = m_varNameTextBox.getText();
      String newVariableValue = m_varValueTextBox.getText();

      String oldVariableName = m_varName;
      String oldVariableValue = m_varValue;

      m_varName = newVariableName;
      m_varValue = newVariableValue;

      //TODO: PageEvent define changed
    }
  }
}
